<!DOCTYPE html>
<html lang="en-gb">

<head>
    <meta charset="UTF-8">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="Simple web framework written by rust">
    <meta name="author" content="Chrislearn Young">
    <title>Hello World | Salvo </title>
    <link rel="stylesheet" href="http:&#x2F;&#x2F;127.0.0.1:1111&#x2F;site.css" />
    <link rel="icon" href="http:&#x2F;&#x2F;127.0.0.1:1111&#x2F;favicon.ico">
</head>

<body>

    <header>
        <nav class="">
            <a class="header__logo white" href="http:&#x2F;&#x2F;127.0.0.1:1111&#x2F;">Savlo</a>
            <a class="white" href="http:&#x2F;&#x2F;127.0.0.1:1111&#x2F;guide&#x2F;" class="nav-link">Guide</a>
            <a class="white" href="https://github.com/kenorld/salvo" class="nav-link">GitHub</a>

            <div class="search-container">
                <input id="search" type="search" placeholder="🔎 Search the docs">

                <div class="search-results">
                    <div class="search-results__items"></div>
                </div>
            </div>
        </nav>
    </header>

    <div class="content content--reversed">
        

<div class="guide">
    <aside class="guide__sidebar">
        <ul>
            
            
            <li>
                <span class="guide__sidebar__title">Getting Started</span>
                <ul>
                    
                    <li class="active">
                        <a href="http:&#x2F;&#x2F;127.0.0.1:1111&#x2F;guide&#x2F;getting-started&#x2F;hello-world&#x2F;">Hello World</a>
                    </li>
                    
                </ul>
            </li>
            
            
            <li>
                <span class="guide__sidebar__title">Content</span>
                <ul>
                    
                </ul>
            </li>
            
        </ul>
    </aside>
    <div class="guide__content">
        
<h1>Hello World</h1>
<p>Create a new rust project:</p>
<pre style="background-color:#383838;">
<span style="color:#e6e1dc;">cargo new salvo_taste</span><span style="font-style:italic;color:#fd971f;"> --bin</span><span style="color:#e6e1dc;">
</span></pre>
<p>Add this to <code>Cargo.toml</code></p>
<pre style="background-color:#383838;">
<span style="color:#e6e1dc;">[</span><span style="color:#e8bf6a;">dependencies</span><span style="color:#e6e1dc;">]</span><span style="color:#e6e1dc;">
</span><span style="color:#e8bf6a;">salvo </span><span style="color:#e6e1dc;">= </span><span style="color:#c1be91;">&quot;0.2&quot;</span><span style="color:#e6e1dc;">
</span><span style="color:#e8bf6a;">tokio </span><span style="color:#e6e1dc;">= { </span><span style="color:#e8bf6a;">version </span><span style="color:#e6e1dc;">= </span><span style="color:#c1be91;">&quot;0.2&quot;</span><span style="color:#e6e1dc;">, </span><span style="color:#e8bf6a;">features </span><span style="color:#e6e1dc;">= [</span><span style="color:#c1be91;">&quot;full&quot;</span><span style="color:#e6e1dc;">] }</span><span style="color:#e6e1dc;">
</span></pre>
<p>Create a simple function handler in the main.rs file, we call it <code>hello_world</code>, this function just render plain text &quot;Hello World&quot;.</p>
<pre style="background-color:#383838;">
<span style="color:#cc7833;">use </span><span style="color:#e6e1dc;">salvo::prelude::</span><span style="color:#cc7833;">*</span><span style="color:#e6e1dc;">;</span><span style="color:#e6e1dc;">
</span><span style="color:#e6e1dc;">
#[fn_handler]</span><span style="color:#e6e1dc;">
async </span><span style="font-style:italic;color:#6e9cbe;">fn </span><span style="color:#ffc66d;">hello_world</span><span style="color:#e6e1dc;">(</span><span style="font-style:italic;color:#fd971f;">_conf</span><span style="color:#e6e1dc;">: Arc&lt;ServerConfig&gt;, </span><span style="font-style:italic;color:#fd971f;">_req</span><span style="color:#e6e1dc;">: </span><span style="color:#cc7833;">&amp;mut</span><span style="color:#e6e1dc;"> Request, </span><span style="font-style:italic;color:#fd971f;">_depot</span><span style="color:#e6e1dc;">: </span><span style="color:#cc7833;">&amp;mut</span><span style="color:#e6e1dc;"> Depot, </span><span style="font-style:italic;color:#fd971f;">resp</span><span style="color:#e6e1dc;">: </span><span style="color:#cc7833;">&amp;mut</span><span style="color:#e6e1dc;"> Response) {</span><span style="color:#e6e1dc;">
    resp.</span><span style="color:#da4939;">render_plain_text</span><span style="color:#e6e1dc;">(</span><span style="color:#c1be91;">&quot;Hello World&quot;</span><span style="color:#e6e1dc;">);</span><span style="color:#e6e1dc;">
}</span><span style="color:#e6e1dc;">
</span></pre>
<p>In the main function, we need to create a root Router first, and then create a server and call it's serve function:</p>
<pre style="background-color:#383838;">
<span style="color:#cc7833;">use </span><span style="color:#e6e1dc;">salvo::prelude::</span><span style="color:#cc7833;">*</span><span style="color:#e6e1dc;">;</span><span style="color:#e6e1dc;">
</span><span style="color:#e6e1dc;">
#[fn_handler]</span><span style="color:#e6e1dc;">
async </span><span style="font-style:italic;color:#6e9cbe;">fn </span><span style="color:#ffc66d;">hello_world</span><span style="color:#e6e1dc;">(</span><span style="font-style:italic;color:#fd971f;">_conf</span><span style="color:#e6e1dc;">: Arc&lt;ServerConfig&gt;, </span><span style="font-style:italic;color:#fd971f;">_req</span><span style="color:#e6e1dc;">: </span><span style="color:#cc7833;">&amp;mut</span><span style="color:#e6e1dc;"> Request, </span><span style="font-style:italic;color:#fd971f;">_depot</span><span style="color:#e6e1dc;">: </span><span style="color:#cc7833;">&amp;mut</span><span style="color:#e6e1dc;"> Depot, </span><span style="font-style:italic;color:#fd971f;">resp</span><span style="color:#e6e1dc;">: </span><span style="color:#cc7833;">&amp;mut</span><span style="color:#e6e1dc;"> Response) {</span><span style="color:#e6e1dc;">
    resp.</span><span style="color:#da4939;">render_plain_text</span><span style="color:#e6e1dc;">(</span><span style="color:#c1be91;">&quot;Hello World&quot;</span><span style="color:#e6e1dc;">);</span><span style="color:#e6e1dc;">
}</span><span style="color:#e6e1dc;">
</span><span style="color:#e6e1dc;">
#[tokio::main]</span><span style="color:#e6e1dc;">
async </span><span style="font-style:italic;color:#6e9cbe;">fn </span><span style="color:#ffc66d;">main</span><span style="color:#e6e1dc;">() -&gt; Result&lt;(), Box&lt;dyn std::error::Error </span><span style="color:#cc7833;">+</span><span style="color:#e6e1dc;"> Send </span><span style="color:#cc7833;">+</span><span style="color:#e6e1dc;"> Sync&gt;&gt; {</span><span style="color:#e6e1dc;">
    </span><span style="font-style:italic;color:#6e9cbe;">let </span><span style="color:#cc7833;">mut</span><span style="color:#e6e1dc;"> router </span><span style="color:#cc7833;">= </span><span style="color:#e6e1dc;">Router::new(</span><span style="color:#c1be91;">&quot;/&quot;</span><span style="color:#e6e1dc;">);</span><span style="color:#e6e1dc;">
    router.</span><span style="color:#da4939;">get</span><span style="color:#e6e1dc;">(hello_world);</span><span style="color:#e6e1dc;">
    </span><span style="font-style:italic;color:#6e9cbe;">let</span><span style="color:#e6e1dc;"> server </span><span style="color:#cc7833;">= </span><span style="color:#e6e1dc;">Server::new(router);</span><span style="color:#e6e1dc;">
    server.</span><span style="color:#da4939;">serve</span><span style="color:#e6e1dc;">().await</span><span style="color:#cc7833;">?</span><span style="color:#e6e1dc;">;</span><span style="color:#e6e1dc;">
    </span><span style="font-style:italic;color:#6e9cbe;">Ok</span><span style="color:#e6e1dc;">(())</span><span style="color:#e6e1dc;">
}</span><span style="color:#e6e1dc;">
</span></pre>

    </div>
</div>

    </div>
    <footer>
        ©2019-2020 — <a class="white" href="http://www.chrislearn.im">Chrislearn Young</a> and <a class="white"
            href="https://github.com/kenorld/salvo/graphs/contributors">contributors</a>
    </footer>

    <script type="text/javascript" src="http:&#x2F;&#x2F;127.0.0.1:1111&#x2F;elasticlunr.min.js"></script>
    <script type="text/javascript" src="http:&#x2F;&#x2F;127.0.0.1:1111&#x2F;search_index.en.js"></script>
    <script type="text/javascript" src="http:&#x2F;&#x2F;127.0.0.1:1111&#x2F;search.js"></script>
<script src="/livereload.js?port=1025&amp;mindelay=10"></script></body>

</html>